Ask a Question
Send an email to me.
 
Java Question and Answer Forums
JavaRanch Big Moose Saloon
Marcus Green's Discussion Forum
java.sun.com Forums, Chat and User Groups
Artima Java Answers Forum
 
Other Resources
Java Language Specification
Java Virtual Machine Specification
Java 2 Platform, Standard Edition, v 1.4.0 API Specification
 
Tutorials
Learning the Java Language
Operator Precedence Chart, Expressions, Statements, Blocks
Programming with Assertions
 
Answers: Certified Java Programmer Mock Exam
No.AnswerRemark
1a  b  e  f  g  Prints: XY  Prints: YX  Nothing is printed.  There is no guarantee that the garbage collector will run.  There is no guarantee that F.main will run to completion.  The finalize method of each instance can only run once so X or Y can never be printed more than once. Although instance x1 becomes eligible for garbage collection first, there is no guarantee that it will be garbage collected first. Even though System.gc is invoked, there is no guarantee that the garbage collector will run. The F.main method stays in a loop until the finalize method of both object instances is invoked. If the garbage collector does not run, then F.main will remain in the loop.  
2c  4  Method K.m1 creates five objects of type J. Each instance has a name represented by an integer between 0 and 4 inclusive. If the garbage collector does not run then the program will produce no output. If the garbage collector does run then the output of the program could be a series of integers that are the names of four of the five objects. As each new object is created its reference is assigned to the reference variable j. The previously referenced object then becomes eligible for garbage collection. The last object created, 4, is not available for garbage collection until method m1 runs to completion. The while loop in the synchronized block will never complete because J.notFinalized will never return zero. This program is intended to provide a working example of garbage collecting objects referenced by local variables.  
3c  d  Prints: I'll be back!  Prints nothing.  Objects that are finalizer-reachable or finalizable can become reachable again if the finalize method assigns a reference to its instance to a member of another object that is reachable. Please see section 12.6.1 of the Java Language Specification for more information. There is no guarantee that the garbage collector will run so it is possible that the program will wait forever and never print anything. I do not recommend using the finalize method to make an object reachable again.  
4a  b  c  d  e  All three instances of I form a ring where each instance within the ring has a reference to the next instance.  All three instances of I are eligible for garbage collection after references i1, i2 and i3 are set to null.  There is no guarantee that the garbage collector will run.  The order in which the instances in the ring are garbage collected is not guaranteed.  The name of all three instances of I might be printed.  The three instances of I form a ring where each instance of I has a reference to another instance. At the line marked 1 the references i1, i2 and i3 are set to null so the ring is no longer reachable from the user code in method J.m1. At that point the entire ring is eligible for garbage collection. For more information, please see section 12.6.2 of the Java Language Specification.  
5b  c  d  f  The user code can invoke the finalize method more than once.  The garbage collector will never call the finalize method more than once.  The output could include the character X.  The output could include the number 3.  Objects that are finalizer-reachable or finalizable can become reachable again if the finalize method assigns a reference to its instance to a member of another object that is reachable. Please see section 12.6.1 of the Java Language Specification for more information. User code can invoke the finalize method any number of times. The garbage collector will not invoke the finalize method of a particular instance more than once so no more than one X can be printed and a number larger than 3 can not be printed. It is possible that the garbage collector will not run so there is no guarantee that anything will be printed. I do not recommend using the finalize method to make an object reachable again.  
6a  b  e  f  g  Prints: XY  Prints: YX  Nothing is printed.  There is no guarantee that the garbage collector will run.  There is no guarantee that G.main will run to completion.  The finalize method of each instance can only run once so X or Y can never be printed more than once. Although instance x1 becomes eligible for garbage collection first, there is no guarantee that it will be garbage collected first. Even though System.gc is invoked, there is no guarantee that the garbage collector will run. The G.main method stays in a loop until the finalize method of both object instances is invoked. If the garbage collector does not run, then G.main will remain in the loop.  
7b  d  e  The garbage collector will never call the finalize method more than once.  Can not print Arnold's quote more than once.  Could print nothing.  Objects that are finalizer-reachable or finalizable can become reachable again if the finalize method assigns a reference to its instance to a member of another object that is reachable. Please see section 12.6.1 of the Java Language Specification for more information. The garbage collector will not call the finalize method of a particular instance more than once so Arnold's quote can not be printed more than once. It is possible that the garbage collector will not run so there is no guarantee that Arnold's quote will be printed even once. I do not recommend using the finalize method to make an object reachable again.  

Copyright © 2002, Dan Chisholm
All rights reserved.
1